<template>
  <div class="home">
    <div class="left">
      <div class="icon_wrapper">
        <div class="avatar">
          <el-avatar
            :src="userStore.userInfo.avatar"
            shape="square"
          ></el-avatar>
        </div>
        <template v-for="(item, index) in optionStore.homeTabbar" :key="index">
          <span
            class="iconfont icon"
            :class="[
              item.icon,
              item.id === 3 ? 'last_icon' : '',
              activeTabbar === index ? 'active' : ''
            ]"
            @click="changeTabbar(index)"
          ></span>
        </template>
      </div>
    </div>
    <div class="right">
      <div class="content">
        <Router-view></Router-view>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'

import useOptions from '@renderer/pinai/modules/options'
import useUser from '@renderer/pinai/modules/user'
import useChat from '@renderer/pinai/modules/chat'

const optionStore = useOptions()
const router = useRouter()
const route = useRoute()
const userStore = useUser()
const chatStore = useChat()

const activeTabbar = ref(0) // 当前激活的tabbar

onMounted(() => {
  // 向主进程通信切换窗口大小
  window.electron.ipcRenderer.send('changeWindowSize', {
    chat: true,
    loginOrResgist: false
  })
  optionStore.homeTabbar.forEach((item, index) => {
    if (item.path === route.path) {
      console.log(item.path, index)
      activeTabbar.value = index
    }
  })
  // 连接wesocket
  userStore.cannectSocket()
  // 初始化会话列表
  chatStore.initSessionChatList()
})

watch(
  () => route.query,
  (newVal: any) => {
    // console.log(newVal)
    if (newVal?.type === 'user' || newVal?.type === 'group') {
      activeTabbar.value = 0
    }
  }
)

const changeTabbar = (index: number) => {
  activeTabbar.value = index
  // 切换路由
  router.push(optionStore.homeTabbar[index].path)
}
</script>

<style lang="scss" scoped>
.home {
  display: flex;
  height: 100vh;
  .left {
    width: 60px;
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    background-color: #2e2e2e;

    .icon_wrapper {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-top: 30px;

      .avatar {
        width: 40px;
        height: 40px;
        margin-bottom: 30px;
      }

      .icon {
        color: #eee;
        font-size: 20px;
        margin-bottom: 30px;
      }

      .active {
        color: #07c160;
      }

      .last_icon {
        margin-top: auto;
      }
    }
  }

  .right {
    flex: 1;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background-color: #fff;

    .content {
      flex: 1;
    }
  }
}
</style>
